arr = [
    [0, 0, 0, 0, 0],
    [0, 0, 0, 0, 0],
    [0, 0, 0, 0, 0],
    [0, 0, 0, 0, 0],
    [0, 0, 0, 0, 0]
]

count = 25
is_running = False
def reset_arr():
    global count
    count = 25
    global arr
    for r in range(5):
        for c in range(5):
            arr[r][c] = 0
            led.unplot(r, c)

def update_led_by_arr():
    basic.pause(500)
    for r in range(5):
        for c in range(5):
            if arr[r][c] == 1:
                led.plot(r, c)
            else:
                led.unplot(r, c)

def on_any_button_pressed():
    global is_running
    if is_running == False:
        reset_arr()
        start()

input.on_button_pressed(Button.A, on_any_button_pressed)
input.on_button_pressed(Button.B, on_any_button_pressed)

def start():
    global count
    global is_running
    x = 0
    y = 0
    arr[x][y] = 1
    update_led_by_arr()
    count -= 1
    is_running = True
    while count > 0:
        while x + 1 < 5 and arr[x + 1][y] == 0:
            x += 1
            arr[x][y] = 1
            count -= 1
            update_led_by_arr()

        while y - 1 >= 0 and arr[x][y - 1] == 0:
            y -= 1
            arr[x][y] = 1
            count -= 1
            update_led_by_arr()

        while x - 1 >= 0 and arr[x - 1][y] == 0:
            x -= 1
            arr[x][y] = 1
            count -= 1
            update_led_by_arr()

        while y + 1 < 5 and arr[x][y + 1] == 0:
            y += 1
            arr[x][y] = 1
            count -= 1
            update_led_by_arr()

    is_running = False

def on_forever():
    pass
basic.forever(on_forever)

start()
